package com.vinobase.controller.management;

import com.vinobase.model.bean.Country;
import com.vinobase.model.bean.CountryLocal;
import com.vinobase.model.bean.Culture;
import com.vinobase.model.dao.CountryDao;
import com.vinobase.model.dao.CultureDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.SortedSet;

@Controller
@RequestMapping(value="/management/country")
public class MgmtCountryController {

    @Autowired
    private CultureDao cultureDao;

    @Autowired
    private CountryDao dao;

    @ModelAttribute("cultures")
    public SortedSet<Culture> populateCultures() {
        return cultureDao.getList();
    }


    @RequestMapping(value="/create.html", method= RequestMethod.GET)
    protected String getCreateForm(Model model){
        model.addAttribute("country", new CountryForm());
        model.addAttribute("countryLocal", new HashSet<CountryLocalForm>());
        return "management/country/create";
    }

    @RequestMapping(value="/addLocal.html", method= RequestMethod.POST)
    protected String addLocal(@ModelAttribute CountryForm country, @ModelAttribute HashSet<CountryLocalForm> countryLocal,  Model model){
        if(country.getId()==null){
            country.setId(new Date().getTime());
        }
        Country c = new Country();
        c.setId(country.getId());
        c.setGlobal(true);
        dao.create(c);

        CountryLocal cl = new CountryLocal();
        cl.setName(country.getName());
        dao.create(cl,country.getCultureId(),country.getId() );

        model.addAttribute("countryLocal", dao.getCountryLocalForms(country.getId()));
        model.addAttribute("country", c.getFormObject());

        return "/management/country/create";
    }

    @RequestMapping(value="/list.html")
    protected String getList(Model model) {
        model.addAttribute("countries",dao.getList());
        return "management/country/list";
    }

    @RequestMapping(value="/{id}.html", method=RequestMethod.GET)
    protected String view(@PathVariable Long id, Model model){

        model.addAttribute("country", dao.get(id).getFormObject());
        model.addAttribute("countryLocal", dao.getCountryLocalForms(id));
        return "management/country/create";
    }

    @RequestMapping(value="/delete.html", method=RequestMethod.GET)
    protected String delete(HttpServletRequest request, Model model){
        dao.delete(Long.parseLong(request.getParameter("id")));

        model.addAttribute("countries",dao.getList());
        return "management/country/list";

    }


}
